package com.ruoyi.leaveManage.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.ruoyi.leaveManage.bo.LeaveUserAddBo;
import com.ruoyi.leaveManage.bo.LeaveUserEditBo;
import com.ruoyi.leaveManage.bo.LeaveUserQueryBo;
import com.ruoyi.leaveManage.domain.LeaveUser;
import com.ruoyi.leaveManage.mapper.LeaveUserMapper;
import com.ruoyi.leaveManage.service.ILeaveUserService;
import com.ruoyi.leaveManage.vo.LeaveUserVo;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.github.pagehelper.Page;

import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 请假附属Service业务层处理
 *
 * @author ruoyi
 * @date 2021-04-26
 */
@Service
public class LeaveUserServiceImpl extends ServiceImpl<LeaveUserMapper, LeaveUser> implements ILeaveUserService {

    @Override
    public LeaveUserVo queryById(Long leaveUserId){
        LeaveUser db = this.baseMapper.selectById(leaveUserId);
        return BeanUtil.toBean(db, LeaveUserVo.class);
    }

    @Override
    public List<LeaveUserVo> queryList(LeaveUserQueryBo bo) {
        LambdaQueryWrapper<LeaveUser> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getLeaveId() != null, LeaveUser::getLeaveId, bo.getLeaveId());
        lqw.eq(bo.getStartTime() != null, LeaveUser::getStartTime, bo.getStartTime());
        lqw.eq(bo.getEndTime() != null, LeaveUser::getEndTime, bo.getEndTime());
        lqw.eq(bo.getIsDelete() != null, LeaveUser::getIsDelete, bo.getIsDelete());
        lqw.eq(bo.getCancellationStatus() != null, LeaveUser::getCancellationStatus, bo.getCancellationStatus());
        return entity2Vo(this.list(lqw));
    }

    /**
    * 实体类转化成视图对象
    *
    * @param collection 实体类集合
    * @return
    */
    private List<LeaveUserVo> entity2Vo(Collection<LeaveUser> collection) {
        List<LeaveUserVo> voList = collection.stream()
                .map(any -> BeanUtil.toBean(any, LeaveUserVo.class))
                .collect(Collectors.toList());
        if (collection instanceof Page) {
            Page<LeaveUser> page = (Page<LeaveUser>)collection;
            Page<LeaveUserVo> pageVo = new Page<>();
            BeanUtil.copyProperties(page,pageVo);
            pageVo.addAll(voList);
            voList = pageVo;
        }
        return voList;
    }

    @Override
    public Boolean insertByAddBo(LeaveUserAddBo bo) {
        LeaveUser add = BeanUtil.toBean(bo, LeaveUser.class);
        validEntityBeforeSave(add);
        return this.save(add);
    }

    @Override
    public Boolean updateByEditBo(LeaveUserEditBo bo) {
        LeaveUser update = BeanUtil.toBean(bo, LeaveUser.class);
        validEntityBeforeSave(update);
        return this.updateById(update);
    }

    /**
     * 保存前的数据校验
     *
     * @param entity 实体类数据
     */
    private void validEntityBeforeSave(LeaveUser entity){
        //TODO 做一些数据校验,如唯一约束
    }

    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return this.removeByIds(ids);
    }
}
